// pages/hospital/users.js
//获取app对象
const app = getApp()
//引入api接口
const {
	getIndexWorkers,
	getIndexOrders,
	receiveOrder
} = require('../../utils/api')
//引入公共方法
const {
	returnBack
} = require('../../utils/util')
//轮播图地址
const swiperList = [
	`/static/index/首页_1.png`,
	`/static/index/首页_2.png`,
	`/static/index/首页_3.png`,
	`/static/index/首页_4.png`,
];

Page({
	data: {
		currentUser: '', //当前登录用户
		workers: [], //优质康复师
		orders: [], //用户订单

		// 服务选项
		service: [
			{
				name: '康复治疗',
				orderTypeId: 1,
				image: '/static/index/康复治疗.png'
			}, 
			{
				name: '一对一服务',
				orderTypeId: 2,
				image: '/static/index/一对一服务.png'
			}, 
			{
				name: '交流论坛',
				orderTypeId: 3,
				image: '/static/index/讨论.png'
			}
		], 

		// 轮播图配置
		current: 0,
		autoplay: true,
		duration: 500,
		interval: 5000,
		swiperList,

		// 选择康复师配置
		chooseWorker: false,
		workerBtn: {
			content: '下单',
			variant: 'base'
		},
		clickWorker: '', // 选择的康复师

		// 选择用户订单配置
		showConfirm: false,
		confirmBtn: {
			content: '接单',
			variant: 'base'
		},
		clickOrder: '', // 选择的订单
	},
	/** 用户下单确认 */
	// 弹出确认框
	userConfirm(e) {
		console.log(e.currentTarget.dataset.worker);
		this.setData({
			chooseWorker: true,
			clickWorker: e.currentTarget.dataset.worker
		})
	},
	// 关闭确认框
	closeConfirm() {
		this.setData({
			chooseWorker: false
		})
	},
	// 确认下单跳转详情页面
	confirmWorker() {
		if (this.data.currentUser == '') {
			wx.redirectTo({
				url: '/pages/login/login',
			}).then(() => {
				wx.showToast({
					title: '请先登录',
					icon: 'error'
				})
			})
		} else {
			const workerId = this.data.clickWorker.id
			const workerName = this.data.clickWorker.name
			console.log(workerId);
			console.log(workerName);
			wx.navigateTo({
				url: '/pages/service/service?workerId=' + workerId + '&workerName=' + workerName,
			})
			this.setData({
				chooseWorker: false
			})
		}
	},

	/** 康复师接单确认 */
	// 弹出确认框
	confirm(e) {
		console.log(e.currentTarget.dataset.member);
		this.setData({
			showConfirm: true,
			clickOrder: e.currentTarget.dataset.member
		})
	},
	// 关闭确认框
	closeDialog() {
		this.setData({
			showConfirm: false
		});
	},
	// 确认接单
	confirmDialog() {
		console.log(this.data.clickOrder.id);
		const orderId = this.data.clickOrder.id
		receiveOrder(orderId).then((res) => {
			console.log(res);
			// 把更新后的订单列表保存进去，关闭选框
			this.setData({
				orders: res.data,
				showConfirm: false
			})
		})
	},

	favourable(e) {
		wx.navigateTo({
		  url: '/pages/favourable/favourable',
		})
	},

	//服务跳转
	service(e) {
		console.log(e.currentTarget.dataset.ordertypeid);
		const orderTypeId = e.currentTarget.dataset.ordertypeid
		//没登陆就跳转到登陆页面
		if (app.globalData.currentUser == '') {
			returnBack('/pages/login/login', '请先登陆', 'error')
		} else {
			//判断是哪个服务，跳转到相应页面
			if (orderTypeId == 1) {
				console.log("康复治疗");
				wx.navigateTo({
				  url: '/pages/recovery/recovery',
				})
			}
			if (orderTypeId == 2) {
				console.log("一对一服务");
				wx.navigateTo({
				  url: '/pages/oneToOne/oneToOne',
				})
			}
			if (orderTypeId == 3) {
				console.log("交流论坛");
				wx.navigateTo({
				  url: '/pages/forum/forum',
				})
			}
			// wx.navigateTo({
			// 	url: '/pages/service/service?orderTypeId=' + orderTypeId,
			// })
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad() {
		this.setData({
			currentUser: app.globalData.currentUser
		})
		//查康复师列表
		getIndexWorkers().then((res) => {
			//此时的res是后端定义的ResultVO模板，把它里面的data取出来才是需要的数据
			console.log(res.data);
			this.setData({
				workers: res.data
			})
		})
		//查用户订单
		getIndexOrders().then((res) => {
			console.log(res.data);
			this.setData({
				orders: res.data
			})
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})